﻿using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using WebChatApi.Common;

namespace WebChatApi
{
    /// <summary>
    /// Extension class for collection type object.
    /// </summary>
    public static class CollectionExtension
    {
        /// <summary>
        /// The format
        /// </summary>
        const string format = "&{0}={1}";

        /// <summary>
        /// To the key value string.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="encodeHtml">if set to <c>true</c> [encode HTML].</param>
        /// <returns>System.String.</returns>
        public static string ToKeyValueString(this IDictionary<string, string> dictionary, bool encodeHtml = false)
        {
            StringBuilder builder = new StringBuilder();

            if (dictionary != null)
            {
                foreach (var key in dictionary.Keys)
                {
                    builder.AppendFormat(format, key, encodeHtml ? WebUtility.HtmlEncode(dictionary[key]) : dictionary[key]);
                }

                if (builder.Length > 0)
                {
                    builder.Remove(0, 1);
                }
            }

            return builder.ToString();
        }

        /// <summary>
        /// To the key value string.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="encodeHtml">if set to <c>true</c> [encode HTML].</param>
        /// <returns>System.String.</returns>
        public static string ToKeyValueString(this NameValueCollection dictionary, bool encodeHtml = false)
        {
            StringBuilder builder = new StringBuilder();

            if (dictionary != null)
            {
                foreach (string key in dictionary.Keys)
                {
                    builder.AppendFormat(format, key, encodeHtml ? WebUtility.HtmlEncode(dictionary[key]) : dictionary[key]);
                }

                if (builder.Length > 0)
                {
                    builder.Remove(0, 1);
                }
            }

            return builder.ToString();
        }


        /// <summary>
        /// Fills the values by key value string.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="keyValueString">The key value string.</param>
        public static void FillValuesByKeyValueString(this NameValueCollection dictionary, string keyValueString)
        {
            if (dictionary != null)
            {
                if (!string.IsNullOrWhiteSpace(keyValueString))
                {
                    var webDictionary = WebUtil.Instance.ConvertKeyValuePairCollectionFromString(keyValueString);
                    foreach (string key in dictionary.Keys)
                    {
                        dictionary.Add(key, webDictionary[key]);
                    }
                }
            }
        }
    }
}
